﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Runtime.Serialization;
using Microsoft.Xna.Framework.Graphics;
using System.IO;

namespace Dark_Dungeon
{
    [Serializable()]
    public class Character : Entity
    {
        /******************************
         *   Inventario                     *
         *   Atributos                      *
         *   Comportamiento          *
         ******************************/

        protected int baseAttack = 1;
        protected int baseDefence = 1;

        // Modificar el ataque y la defensa con el arma, hechizos, objetos mágicos, etc
        public int attack
        {
            get { return baseAttack; }
        }

        public int defence
        {
            get { return baseDefence; }
        }

        protected EntityDirection attackDirection = EntityDirection.NoDirection;

        protected float playerdistance = 0.0f;

        #region -- Constructor --
        public Character(Vector2 Position) : base(Position)
        {
        }
        #endregion

        public EntityDirection Attacking()
        {
            return attackDirection;
        }

        public void AttackEnemy(ref Character enemy)
        {
            int damage = enemy.TakeDamage(this.attack);
            Debug.AddStaticText(enemy.Name + " recibe " + damage + " puntos de daño.");
        }

        public int TakeDamage(int attack)
        {
            int damage = attack - this.defence;
            if (damage > 0)
                this.HitPoints -= damage;
            return damage;
        }


        protected float PlayerDistance(Player player)
        {
            return Vector2.Distance(this.position, player.position);
        }

        public virtual void Update(GameTime gameTime, MapEngine.TileMap map, ref Player player)
        {
            playerdistance = PlayerDistance(player);
            base.Update(gameTime, map);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(Resources.TerminalFont, playerdistance.ToString("f2"), new Vector2(position.X * 32, position.Y*32), Color.Yellow, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, Config.TextLayer);

            base.Draw(gameTime, spriteBatch);
        }

        #region -- Serialización/Deserialización --
        public Character(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            baseAttack = (int)info.GetValue("baseAttack", typeof(int));
            baseDefence = (int)info.GetValue("baseDefence", typeof(int));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {

            info.AddValue("baseAttack", baseAttack);
            info.AddValue("baseDefence", baseDefence);

            base.GetObjectData(info, ctxt);
        }
        #endregion

        #region -- Ficheros de Characters --

        static List<CharactersData> CharactersDataList = new List<CharactersData>();

        static public void LoadCharacterFile()
        {
            using (StreamReader reader = new StreamReader(@"Content\Data\Characters.cdf"))
            {
                CharactersData characterdata = new CharactersData();
                string line;

                line = reader.ReadLine();
                if (line != "cdf")
                    throw new Exception("Cabecera de fichero incorrecta.");

                line = reader.ReadLine();
                if (line != Config.Version)
                    throw new Exception("Versión del fichero incorrecta.");

                while ((line = reader.ReadLine()) != null)
                {
                    String[] fields = line.Split(';');
                    characterdata.type = fields[0];
                    characterdata.name = fields[1];
                    characterdata.tilex = Convert.ToInt32(fields[2]);
                    characterdata.tiley = Convert.ToInt32(fields[3]);
                    characterdata.baseattack = Convert.ToInt32(fields[4]);
                    characterdata.basedefence = Convert.ToInt32(fields[5]);

                    CharactersDataList.Add(characterdata);
                }
            }
        }

        static public CharactersData LoadCharacterData(string type)
        {
            return CharactersDataList.Find(p => p.type == type);
        }

        #endregion

    }

    public struct CharactersData
    {
        public string type;
        public string name;
        public int tilex;
        public int tiley;
        public int baseattack;
        public int basedefence;
    }
}
